#include "rwmake.ch"
#include "topconn.ch"


User Function ALPH500() //chamada Via Menu

Processa({|| CORRSD2(),'CORRSD2'})
alert("fim")
Return

STATIC Function CORRSB1()

cQuery:='' //QUERY 
cQuery :=" SELECT * FROM "
cQuery +=  RetSqlName("SB1") + " SB1 WHERE B1_SKUNUM <> ' ' AND "
cQuery += " SB1.D_E_L_E_T_ <> '*'"
cQuery += " Order BY SB1.B1_COD "
cQuery:=ChangeQuery(cQuery)

if !empty(Select("TB1"))
	dbSelectArea("TB1")
	dbCloseArea()
endif
TCQuery cQuery NEW ALIAS "TB1"

DbSelectArea("TB1")
DbGoTop()
Procregua(Reccount())

While !EOF()
	
	INCPROC("SB2010 "+TB1->B1_COD)
	
	cALIAS:='SB2'
	cQuery:=''
	cQuery :=" SELECT R_E_C_N_O_ RECNOX  FROM "
	cQuery +=  RetSqlName("SB2") + " SB2 WHERE B2_COD = '"+TB1->B1_COD+"'"
	cQuery += " AND SB2.D_E_L_E_T_ <> '*'"
	cQuery:=ChangeQuery(cQuery)
	
	if !empty(Select("TBX"))
		dbSelectArea("TBX")
		dbCloseArea()
	endif
	TCQuery cQuery NEW ALIAS "TBX"
	
	DbSelectArea("TBX")
	DbGoTop()
	While !EOF()
		Dbselectarea("SB2")
			Dbgoto(TBX->Recnox)
		Reclock("SB2",.F.)
		SB2->B2_COD:=TB1->B1_SKUNUM
		Msunlock()
		DbSelectArea("TBX")
		Dbskip()
	Enddo
	DbSelectArea("TB1")
	Dbskip()
Enddo


DbSelectArea("TB1")
DbGoTop()
Procregua(Reccount())

While !EOF()
	
	INCPROC("SB5010 "+TB1->B1_COD)
	
	cALIAS:='SB5'
	cQuery:=''
	cQuery :=" SELECT R_E_C_N_O_ RECNOX  FROM "
	cQuery +=  RetSqlName("SB5") + " SB5 WHERE B5_COD = '"+TB1->B1_COD+"'"
	cQuery += " AND SB5.D_E_L_E_T_ <> '*'"
	cQuery:=ChangeQuery(cQuery)
	
	if !empty(Select("TBX"))
		dbSelectArea("TBX")
		dbCloseArea()
	endif
	TCQuery cQuery NEW ALIAS "TBX"
	
	DbSelectArea("TBX")
	DbGoTop()
	While !EOF()
		Dbselectarea("SB5")
			Dbgoto(TBX->Recnox)
		Reclock("SB5",.F.)
		SB5->B5_COD:=TB1->B1_SKUNUM
		Msunlock()
		DbSelectArea("TBX")
		Dbskip()
	Enddo
	DbSelectArea("TB1")
	Dbskip()
Enddo


DbSelectArea("TB1")
DbGoTop()
Procregua(Reccount())

While !EOF()
	
	INCPROC("SB6010 "+TB1->B1_COD)
	
	cALIAS:='SB6'
	cQuery:=''
	cQuery :=" SELECT R_E_C_N_O_ RECNOX  FROM "
	cQuery +=  RetSqlName("SB6") + " SB6 WHERE B6_PRODUTO = '"+TB1->B1_COD+"'"
	cQuery += " AND SB6.D_E_L_E_T_ <> '*'"
	cQuery:=ChangeQuery(cQuery)
	
	if !empty(Select("TBX"))
		dbSelectArea("TBX")
		dbCloseArea()
	endif
	TCQuery cQuery NEW ALIAS "TBX"
	
	DbSelectArea("TBX")
	DbGoTop()
	While !EOF()
		Dbselectarea("SB6")
			Dbgoto(TBX->Recnox)
		Reclock("SB6",.F.)
		SB6->B6_PRODUTO:=TB1->B1_SKUNUM
		Msunlock()
		DbSelectArea("TBX")
		Dbskip()
	Enddo
	DbSelectArea("TB1")
	Dbskip()
Enddo


DbSelectArea("TB1")
DbGoTop()
Procregua(Reccount())

While !EOF()
	
	INCPROC("SB8010 "+TB1->B1_COD)
	
	cALIAS:='SB8'
	cQuery:=''
	cQuery :=" SELECT R_E_C_N_O_ RECNOX  FROM "
	cQuery +=  RetSqlName("SB8") + " SB8 WHERE B8_PRODUTO = '"+TB1->B1_COD+"'"
	cQuery += " AND SB8.D_E_L_E_T_ <> '*'"
	cQuery:=ChangeQuery(cQuery)
	
	if !empty(Select("TBX"))
		dbSelectArea("TBX")
		dbCloseArea()
	endif
	TCQuery cQuery NEW ALIAS "TBX"
	
	DbSelectArea("TBX")
	DbGoTop()
	While !EOF()
		Dbselectarea("SB8")
			Dbgoto(TBX->Recnox)
		Reclock("SB8",.F.)
		SB8->B8_PRODUTO:=TB1->B1_SKUNUM
		Msunlock()
		DbSelectArea("TBX")
		Dbskip()
	Enddo
	DbSelectArea("TB1")
	Dbskip()
Enddo


DbSelectArea("TB1")
DbGoTop()
Procregua(Reccount())

While !EOF()
	
	INCPROC("SB9010 "+TB1->B1_COD)
	
	cALIAS:='SB9'
	cQuery:=''
	cQuery :=" SELECT R_E_C_N_O_ RECNOX  FROM "
	cQuery +=  RetSqlName("SB9") + " SB9 WHERE B9_COD = '"+TB1->B1_COD+"'"
	cQuery += " AND SB9.D_E_L_E_T_ <> '*'"
	cQuery:=ChangeQuery(cQuery)
	
	if !empty(Select("TBX"))
		dbSelectArea("TBX")
		dbCloseArea()
	endif
	TCQuery cQuery NEW ALIAS "TBX"
	
	DbSelectArea("TBX")
	DbGoTop()
	While !EOF()
		Dbselectarea("SB9")
			Dbgoto(TBX->Recnox)
		Reclock("SB9",.F.)
		SB9->B9_COD:=TB1->B1_SKUNUM
		Msunlock()
		DbSelectArea("TBX")
		Dbskip()
	Enddo
	DbSelectArea("TB1")
	Dbskip()
Enddo


DbSelectArea("TB1")
DbGoTop()
Procregua(Reccount())

While !EOF()
	
	INCPROC("SC6010 "+TB1->B1_COD)
	
	cALIAS:='SC6'
	cQuery:=''
	cQuery :=" SELECT R_E_C_N_O_ RECNOX  FROM "
	cQuery +=  RetSqlName("SC6") + " SC6 WHERE C6_PRODUTO = '"+TB1->B1_COD+"'"
	cQuery += " AND SC6.D_E_L_E_T_ <> '*'"
	cQuery:=ChangeQuery(cQuery)
	
	if !empty(Select("TBX"))
		dbSelectArea("TBX")
		dbCloseArea()
	endif
	TCQuery cQuery NEW ALIAS "TBX"
	
	DbSelectArea("TBX")
	DbGoTop()
	While !EOF()
		Dbselectarea("SC6")
			Dbgoto(TBX->Recnox)
		Reclock("SC6",.F.)
		SC6->C6_PRODUTO:=TB1->B1_SKUNUM
		Msunlock()
		DbSelectArea("TBX")
		Dbskip()
	Enddo
	DbSelectArea("TB1")
	Dbskip()
Enddo

DbSelectArea("TB1")
DbGoTop()
Procregua(Reccount())

While !EOF()
	
	INCPROC("SD1010 "+TB1->B1_COD)
	
	cALIAS:='SD1'
	cQuery:=''
	cQuery :=" SELECT R_E_C_N_O_ RECNOX  FROM "
	cQuery +=  RetSqlName("SD1") + " SD1 WHERE D1_COD = '"+TB1->B1_COD+"'"
	cQuery += " AND SD1.D_E_L_E_T_ <> '*'"
	cQuery:=ChangeQuery(cQuery)
	
	if !empty(Select("TBX"))
		dbSelectArea("TBX")
		dbCloseArea()
	endif
	TCQuery cQuery NEW ALIAS "TBX"
	
	DbSelectArea("TBX")
	DbGoTop()
	While !EOF()
		Dbselectarea("SD1")
			Dbgoto(TBX->Recnox)
		Reclock("SD1",.F.)
		SD1->D1_COD:=TB1->B1_SKUNUM
		Msunlock()
		DbSelectArea("TBX")
		Dbskip()
	Enddo
	DbSelectArea("TB1")
	Dbskip()
Enddo



DbSelectArea("TB1")
DbGoTop()
Procregua(Reccount())

While !EOF()
	
	INCPROC("ZZZZ10 "+TB1->B1_COD)
	
	cALIAS:='SD2'
	cQuery:=''
	cQuery :=" SELECT R_E_C_N_O_ RECNOX  FROM "
	cQuery +=  RetSqlName("SD2") + " SD2 WHERE D2_COD = '"+TB1->B1_COD+"'"
	cQuery += " AND SD2.D_E_L_E_T_ <> '*'"
	cQuery:=ChangeQuery(cQuery)
	
	if !empty(Select("TBX"))
		dbSelectArea("TBX")
		dbCloseArea()
	endif
	TCQuery cQuery NEW ALIAS "TBX"
	
	DbSelectArea("TBX")
	DbGoTop()
	While !EOF()
		Dbselectarea("SD2")
			Dbgoto(TBX->Recnox)
		Reclock("SD2",.F.)
		SD2->D2_COD:=TB1->B1_SKUNUM
		Msunlock()
		DbSelectArea("TBX")
		Dbskip()
	Enddo
	DbSelectArea("TB1")
	Dbskip()
Enddo


DbSelectArea("TB1")
DbGoTop()
Procregua(Reccount())

While !EOF()
	
	INCPROC("SD3010 "+TB1->B1_COD)
	
	cALIAS:='SD3'
	cQuery:=''
	cQuery :=" SELECT R_E_C_N_O_ RECNOX  FROM "
	cQuery +=  RetSqlName("SD3") + " SD3 WHERE D3_COD = '"+TB1->B1_COD+"'"
	cQuery += " AND SD3.D_E_L_E_T_ <> '*'"
	cQuery:=ChangeQuery(cQuery)
	
	if !empty(Select("TBX"))
		dbSelectArea("TBX")
		dbCloseArea()
	endif
	TCQuery cQuery NEW ALIAS "TBX"
	
	DbSelectArea("TBX")
	DbGoTop()
	While !EOF()
		Dbselectarea("SD3")
			Dbgoto(TBX->Recnox)
		Reclock("SD3",.F.)
		SD3->D3_COD:=TB1->B1_SKUNUM
		Msunlock()
		DbSelectArea("TBX")
		Dbskip()
	Enddo
	DbSelectArea("TB1")
	Dbskip()
Enddo



DbSelectArea("TB1")
DbGoTop()
Procregua(Reccount())

While !EOF()
	
	INCPROC("SD5010 "+TB1->B1_COD)
	
	cALIAS:='SD5'
	cQuery:=''
	cQuery :=" SELECT R_E_C_N_O_ RECNOX  FROM "
	cQuery +=  RetSqlName("SD5") + " SD5 WHERE D5_PRODUTO = '"+TB1->B1_COD+"'"
	cQuery += " AND SD5.D_E_L_E_T_ <> '*'"
	cQuery:=ChangeQuery(cQuery)
	
	if !empty(Select("TBX"))
		dbSelectArea("TBX")
		dbCloseArea()
	endif
	TCQuery cQuery NEW ALIAS "TBX"
	
	DbSelectArea("TBX")
	DbGoTop()
	While !EOF()
		Dbselectarea("SD5")
			Dbgoto(TBX->Recnox)
		Reclock("SD5",.F.)
		SD5->D5_PRODUTO:=TB1->B1_SKUNUM
		Msunlock()
		DbSelectArea("TBX")
		Dbskip()
	Enddo
	DbSelectArea("TB1")
	Dbskip()
Enddo


DbSelectArea("TB1")
DbGoTop()
Procregua(Reccount())

While !EOF()
	
	INCPROC("SFT010 "+TB1->B1_COD)
	
	cALIAS:='SFT'
	cQuery:=''
	cQuery :=" SELECT R_E_C_N_O_ RECNOX  FROM "
	cQuery +=  RetSqlName("SFT") + " SFT WHERE FT_PRODUTO = '"+TB1->B1_COD+"'"
	cQuery += " AND SFT.D_E_L_E_T_ <> '*'"
	cQuery:=ChangeQuery(cQuery)
	
	if !empty(Select("TBX"))
		dbSelectArea("TBX")
		dbCloseArea()
	endif
	TCQuery cQuery NEW ALIAS "TBX"
	
	DbSelectArea("TBX")
	DbGoTop()
	While !EOF()
		Dbselectarea("SFT")
			Dbgoto(TBX->Recnox)
		Reclock("SFT",.F.)
		SFT->FT_PRODUTO:=TB1->B1_SKUNUM
		Msunlock()
		DbSelectArea("TBX")
		Dbskip()
	Enddo
	DbSelectArea("TB1")
	Dbskip()
Enddo


DbSelectArea("TB1")
DbGoTop()
Procregua(Reccount())

While !EOF()
	
	INCPROC("SB1010 "+TB1->B1_COD)
	
	cALIAS:='SB1'
	cQuery:=''
	cQuery :=" SELECT R_E_C_N_O_ RECNOX  FROM "
	cQuery +=  RetSqlName("SB1") + " SB1 WHERE B1_COD = '"+TB1->B1_COD+"'"
	cQuery += " AND SB1.D_E_L_E_T_ <> '*'"
	cQuery:=ChangeQuery(cQuery)
	
	if !empty(Select("TBX"))
		dbSelectArea("TBX")
		dbCloseArea()
	endif
	TCQuery cQuery NEW ALIAS "TBX"
	
	DbSelectArea("TBX")
	DbGoTop()
	While !EOF()
		Dbselectarea("SB1")
			Dbgoto(TBX->Recnox)
		Reclock("SB1",.F.)
		SB1->B1_CODBKP:=TB1->B1_COD
		SB1->B1_COD:=TB1->B1_SKUNUM
		Msunlock()
		DbSelectArea("TBX")
		Dbskip()
	Enddo
	DbSelectArea("TB1")
	Dbskip()
Enddo


STATIC Function CORRSd2()

cQuery:='' //QUERY
cQuery := " SELECT R_E_C_N_O_ RECNOX,SD2.* FROM "
cQuery +=    RetSqlName("SD2") + " SD2 WHERE "
cQuery += "  SD2.D_E_L_E_T_ <> '*' "
cQuery += "	ORDER BY SD2.D2_FILIAL,SD2.D2_DOC+SD2.D2_SERIE+SD2.D2_CLIENTE,SD2.D2_LOJA,SD2.D2_FORMUL"
cQuery:=ChangeQuery(cQuery)

if !empty(Select("TD2"))
	dbSelectArea("TD2")
	dbCloseArea()
endif
TCQuery cQuery NEW ALIAS "TD2"

DbSelectArea("TD2")
Procregua(Reccount())
DbGoTop()
_cDoc:=''
_cHVSF2:='' // TD2->D2_FILIAL+TD2->D2_DOC+TD2->D2_SERIE+TD2->D2_CLIENTE+TD2->D2_LOJA+TD2->D2_FORMUL

While !EOF()

    incproc(SD2->D2_DOC+" ---ICMS----")

	_nVTicmr:=0
	_cHVSF2:=TD2->D2_FILIAL+TD2->D2_DOC+TD2->D2_SERIE+TD2->D2_CLIENTE+TD2->D2_LOJA+TD2->D2_FORMUL
	_cDoc:=TD2->D2_DOC
	
	While !EOF() .and. _cHVSF2 == TD2->D2_FILIAL+TD2->D2_DOC+TD2->D2_SERIE+TD2->D2_CLIENTE+TD2->D2_LOJA+TD2->D2_FORMUL
		
		Dbselectarea("SF4")
		Dbsetorder(1)
		Dbseek(xFILIAL("SF4")+TD2->D2_TES)
		
		IF upper(substr(SF4->F4_TEXTO,1,1)) <> 'V'
			DbSelectArea("TD2")
			Dbskip()
			Loop
		Endif
		_nValicmR:=0
		_nValicmR:=TD2->D2_VALICM
		IF SF4->F4_CRICMPR == 'S' //CALCULA PRESUMIDO
			_cCricmPr:=''
			_cCricmPr:=GETADVFVAl("SB1","B1_CRICMPR",XFILIAL("SB1")+TD2->D2_COD,1,'') //CALCULA PRESUMIDO
			IF _cCricmPr =='S'
				_cAliq:=''
				_cAliq:=ALLTRIM(STR(INT(TD2->D2_PICM)))
				_nAliq:=0
				IF _cAliq == '12'
					_nAliq := 0.6666
				Else
					_nAliq := 0.4286
				Endif
				IF _nAliq > 0
					_nValicmR:=TD2->D2_VALICM * _nAliq
					_nValicmR:=TD2->D2_VALICM - _nValicmR
				Endif
			Endif
		Endif
		Dbselectarea("SD2")
		Dbgoto(TD2->RECNOX)
		Reclock("SD2",.F.)
    	SD2->D2_CRICMPR:=TD2->D2_VALICM - _nValicmR
		Msunlock()
		_nVTicmr:=_nVTicmr + SD2->D2_CRICMPR
		Dbselectarea("TD2")
		Dbskip()
	Enddo
	Dbselectarea("SF2")
	Dbsetorder(1)
	IF Dbseek(_cHVSF2)
		Dbselectarea("SF2")
		Reclock("SF2",.F.)
		SF2->F2_CRICMPR:=_nVTicmr
		mSunlock()
	Endif
	Dbselectarea("TD2")
Enddo
dbSelectArea("TD2")
dbCloseArea()
Return



